import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Index from '../views/Index.vue'
const Roles = () =>
    import ('../views/Roles.vue')
const Right = () =>
    import ('../views/Rights.vue')
const Users = () =>
    import ('../views/Users.vue')
const Goods = () =>
    import ('../views/Goods.vue')
const GoodsAdd = () =>
    import ('../views/GoodsAdd.vue')
const Categores = () =>
    import ('../views/Categores.vue')

import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
    { path: '', redirect: "/index" },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/index',
        // name: 'Index',
        component: Index,
        children: [
            { path: '', name: 'Home', component: Home },
            { path: '/users', name: 'Users', component: Users },
            { path: "/roles", name: 'Roles', component: Roles },
            { path: '/rights', name: 'Right', component: Right },
            { path: '/goods', name: 'Goods', component: Goods },
            { path: '/goods-add', name: 'GoodsAdd', component: GoodsAdd },
            { path: '/categories', name: "Categores", component: Categores }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    const tokensign = window.localStorage.getItem('token')
        //to.path 目标路由
        //next 必须有否则程序不会继续执行
    if (to.path !== '/login' && !tokensign) { return next('/login') }
    next()
})
export default router;